{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 1.类和对象的概念"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1.1 类"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 类：具有相同特征或者行为的事物的统称\n",
    "#    --特征 被称为 属性\n",
    "#    --行为 被称为 方法"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1.2 对象"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 对象：由类创造出来的一个具体的存在，可以直接使用"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2.类和对象的关系"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "#• 类是模板，对象 是根据 类 这个模板创建出来的，应该 先有类，再有对象\n",
    "# • 类 只有一个，而 对象 可以有很多个\n",
    "# – 不同的对象 之间 属性 可能会各不相同\n",
    "# • 类 中定义了什么 属性和方法，对象 中就有什么属性和方法，不可能多，也不\n",
    "# 可能少"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 3.类的设计"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 1 类名 这类事物的名字，满足大驼峰命名法\n",
    "# 2 属性 这类事物具有什么样的特征\n",
    "# 3 方法 这类事物具有什么样的行为"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "# 需求\n",
    "# • 小明 今年 18 岁，身高 1.75，每天早上 跑 完步，会去 吃 东西\n",
    "# • 小美 今年 17 岁，身高 1.65，小美不跑步，小美喜欢 吃 东西\n",
    "\n",
    "class Person:\n",
    "    def __init__(self, name, age, height):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        self.height = height\n",
    "\n",
    "    def run(self):\n",
    "        print('%s is running' % self.name)\n",
    "\n",
    "    def eat(self):\n",
    "        print('%s is eating' % self.name)\n",
    "\n",
    "\n",
    "xiaoming = Person('xiaoming', 18, 1.75)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "xiaoming is running\n"
     ]
    }
   ],
   "source": [
    "xiaoming.run()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "xiaoming is eating\n"
     ]
    }
   ],
   "source": [
    "xiaoming.eat()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 需求\n",
    "# • 一只 黄颜色 的 狗狗 叫 大黄\n",
    "# • 看见生人 汪汪叫\n",
    "# • 看见家人 摇尾巴"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "一只黄色的狗叫大黄\n"
     ]
    }
   ],
   "source": [
    "class Animal:\n",
    "    def __init__(self, var, name, color):\n",
    "        self.var = var\n",
    "        self.name = name\n",
    "        self.color = color\n",
    "\n",
    "    def introduce(self):\n",
    "        print(\"一只{}的{}叫{}\".format(self.color, self.var, self.name))\n",
    "\n",
    "    def see(self, people=True):\n",
    "        if people:  #True 熟人\n",
    "            print(\"摇尾巴\")\n",
    "        else:\n",
    "            print(\"汪汪叫\")\n",
    "\n",
    "\n",
    "dog = Animal(\"狗\", \"大黄\", \"黄色\")\n",
    "dog.introduce()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "汪汪叫\n"
     ]
    }
   ],
   "source": [
    "dog.see(people=False)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "摇尾巴\n"
     ]
    }
   ],
   "source": [
    "dog.see()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}